//////////////////////////////////////////////////////////////////////////
///	\file:	SocketReader.h
///	Copyright (MingYou) 2009 - All Rights Reserved
///	Author:	Robert.Ma
///	Date:	[2009/05/14]
///	Description:
///		
//////////////////////////////////////////////////////////////////////////
#pragma once
#include "MThread.h"
#include "Define.h"


class CSocketReader : public CMThread
{
public:
	CSocketReader(void);
	virtual ~CSocketReader(void);

	BOOL Initialize(SOCKET* pSocket, DWORD dwParentID);
	void Release();

	int CopyBuffer(BYTE* pbyGet, int iLen);
	int GetBufferSize();

protected:
	virtual DWORD Execute();

	virtual void PreCreate();
	virtual void AfterCreate();
	virtual void PreClose();
	virtual void AfterClose();

private:
	BOOL	LockBuffer();
	void	UnlockBuffer();
	int		ReadSocketData();
	void	ActiveReadAction(DWORD dwDelay);

	BYTE	m_byCacheBuffer[MAX_BUFFERSIZE];
	BYTE	m_byShadowBuffer[MAX_BUFFERSIZE];
	int		m_iCurBufferSize;

	SOCKET*	m_pSocket;
	char*	m_pRecBuf;
	int		m_iSocketError;
	HANDLE	m_hBufMutex;
	DWORD	m_dwParentID;
};
